home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / make-367.lha / make-3.67 / expand.c < prev    next >
C/C++ Source or Header  |  1993-05-06  |  12KB  |  440 lines

  1. /* Variable expansion functions for GNU Make.
  2. Copyright (C) 1988, 1989, 1991, 1992, 1993 Free Software Foundation, Inc.
  3. This file is part of GNU Make.
  4.  
  5. GNU Make is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2, or (at your option)
  8. any later version.
  9.  
  10. GNU Make is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. GNU General Public License for more details.
  14.  
  15. You should have received a copy of the GNU General Public License
  16. along with GNU Make; see the file COPYING.  If not, write to
  17. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  18.  
  19. #include "make.h"
  20. #include "commands.h"
  21. #include "file.h"
  22. #include "variable.h"
  23.  
  24. /* The next two describe the variable output buffer.
  25.    This buffer is used to hold the variable-expansion of a line of the
  26.    makefile.  It is made bigger with realloc whenever it is too small.
  27.    variable_buffer_length is the size currently allocated.
  28.    variable_buffer is the address of the buffer.  */
  29.  
  30. static unsigned int variable_buffer_length;
  31. static char *variable_buffer;
  32.  
  33. /* Subroutine of variable_expand and friends:
  34.    The text to add is LENGTH chars starting at STRING to the variable_buffer.
  35.    The text is added to the buffer at PTR, and the updated pointer into
  36.    the buffer is returned as the value.  Thus, the value returned by
  37.    each call to variable_buffer_output should be the first argument to
  38.    the following call.  */
  39.  
  40. char *
  41. variable_buffer_output (ptr, string, length)
  42.      char *ptr, *string;
  43.      unsigned int length;
  44. {
  45.   register unsigned int newlen = length + (ptr - variable_buffer);
  46.  
  47.   if (newlen > variable_buffer_length)
  48.     {
  49.       unsigned int offset = ptr - variable_buffer;
  50.       variable_buffer_length = (newlen + 100 > 2 * variable_buffer_length
  51.                 ? newlen + 100
  52.                 : 2 * variable_buffer_length);
  53.       variable_buffer = (char *) xrealloc (variable_buffer,
  54.                        variable_buffer_length);
  55.       ptr = variable_buffer + offset;
  56.     }
  57.  
  58.   bcopy (string, ptr, length);
  59.   return ptr + length;
  60. }
  61.  
  62. /* Return a pointer to the beginning of the variable buffer.  */
  63.  
  64. static char *
  65. initialize_variable_output ()
  66. {
  67.   /* If we don't have a variable output buffer yet, get one.  */
  68.  
  69.   if (variable_buffer == 0)
  70.     {
  71.       variable_buffer_length = 200;
  72.       variable_buffer = (char *) xmalloc (variable_buffer_length);
  73.       variable_buffer[0] = '\0';
  74.     }
  75.  
  76.   return variable_buffer;
  77. }
  78.  
  79. /* Recursively expand V.  The returned string is malloc'd.  */
  80.  
  81. char *
  82. recursively_expand (v)
  83.      register struct variable *v;
  84. {
  85.   char *value;
  86.  
  87.   if (v->expanding)
  88.     {
  89.       /* Expanding V causes infinite recursion.  Lose.  */
  90.       if (reading_filename == 0)
  91.     fatal ("Recursive variable `%s' references itself (eventually)",
  92.            v->name);
  93.       else
  94.     makefile_fatal
  95.       (reading_filename, *reading_lineno_ptr, 
  96.        "Recursive variable `%s' references itself (eventually)",
  97.        v->name);
  98.     }
  99.  
  100.   v->expanding = 1;
  101.   value = allocated_variable_expand (v->value);
  102.   v->expanding = 0;
  103.  
  104.   return value;
  105. }
  106.  
  107. /* Expand a simple reference to variable NAME, which LENGTH chars long.  */
  108.  
  109. #ifdef __GNUC__
  110. __inline
  111. #endif
  112. static char *
  113. reference_variable (o, name, length)
  114.      char *o;
  115.      char *name;
  116.      unsigned int length;
  117. {
  118.   register struct variable *v = lookup_variable (name, length);
  119.  
  120.   if (v != 0 && *v->value != '\0')
  121.     {
  122.       char *value = (v->recursive ? recursively_expand (v) : v->value);
  123.       o = variable_buffer_output (o, value, strlen (value));
  124.       if (v->recursive)
  125.     free (value);
  126.     }
  127.  
  128.   return o;
  129. }
  130.  
  131. /* Scan LINE for variable references and expansion-function calls.
  132.    Build in `variable_buffer' the result of expanding the references and calls.
  133.    Return the address of the resulting string, which is null-terminated
  134.    and is valid only until the next time this function is called.  */
  135.  
  136. char *
  137. variable_expand (line)
  138.      register char *line;
  139. {
  140.   register struct variable *v;
  141.   register char *p, *o, *p1;
  142.  
  143.   p = line;
  144.   o = initialize_variable_output ();
  145.  
  146.   while (1)
  147.     {
  148.       /* Copy all following uninteresting chars all at once to the
  149.          variable output buffer, and skip them.  Uninteresting chars end
  150.      at the next $ or the end of the input.  */
  151.  
  152.       p1 = index (p, '$');
  153.  
  154.       o = variable_buffer_output (o, p, p1 != 0 ? p1 - p : strlen (p) + 1);
  155.  
  156.       if (p1 == 0)
  157.     break;
  158.       p = p1 + 1;
  159.  
  160.       /* Dispatch on the char that follows the $.  */
  161.  
  162.       switch (*p)
  163.     {
  164.     case '$':
  165.       /* $$ seen means output one $ to the variable output buffer.  */
  166.       o = variable_buffer_output (o, p, 1);
  167.       break;
  168.  
  169.     case '(':
  170.     case '{':
  171.       /* $(...) or ${...} is the general case of substitution.  */
  172.       {
  173.         char openparen = *p;
  174.         char closeparen = (openparen == '(') ? ')' : '}';
  175.         register char *beg = p + 1;
  176.         char *op, *begp;
  177.         char *end;
  178.         char *colon = 0;
  179.  
  180.         op = o;
  181.         begp = p;
  182.         if (handle_function (&op, &begp))
  183.           {
  184.         o = op;
  185.         p = begp;
  186.         break;
  187.           }
  188.  
  189.         /* Is there a variable reference inside the parens or braces?
  190.            If so, expand it before expanding the entire reference.  */
  191.  
  192.         p1 = index (beg, closeparen);
  193.         if (p1 != 0)
  194.           p1 = lindex (beg, p1, '$');
  195.         if (p1 != 0 && lindex (beg, p1, ':') == 0)
  196.           {
  197.         /* BEG now points past the opening paren or brace.
  198.            Count parens or braces until it is matched.  */
  199.         int count = 0;
  200.         for (p = beg; *p != '\0'; ++p)
  201.           {
  202.             if (*p == openparen)
  203.               ++count;
  204.             else if (*p == closeparen && --count < 0)
  205.               break;
  206.             else if (colon == 0 && count == 0 && *p == ':')
  207.               /* Record where we found a colon, which
  208.              indicates a substitution reference.
  209.              We want to expand the text before the
  210.              reference only.  */
  211.               colon = p;
  212.           }
  213.         /* If COUNT is >= 0, there were unmatched opening parens
  214.            or braces, so we go to the simple case of a variable name
  215.            such as `$($(a)'.  */
  216.         if (count < 0)
  217.           {
  218.             char *name = expand_argument (beg, colon == 0 ? p : colon);
  219.             unsigned int namelen = strlen (name);
  220.             if (colon == 0)
  221.               {
  222.             /* This is a simple reference to the expanded name.  */
  223.             o = reference_variable (o, name, namelen);
  224.             free (name);
  225.             break;
  226.               }
  227.             else
  228.               {
  229.             /* This is a substitution reference to the expanded
  230.                name.  We replace the pending text with a copy
  231.                containing the expanded name in place of the
  232.                original name, and then fall through to
  233.                the normal substitution reference code below.  */
  234.             unsigned int restlen = strlen (colon) + 1;
  235.             beg = (char *) alloca (namelen + restlen);
  236.             bcopy (name, beg, namelen);
  237.             bcopy (colon, &beg[namelen], restlen);
  238.             /* Point COLON into the new copy.  */
  239.             colon = &beg[namelen];
  240.               }
  241.           }
  242.           }
  243.  
  244.         /* This is not a reference to a built-in function and
  245.            it does not contain any variable references inside.
  246.            There are several things it could be.  */
  247.  
  248.         if (colon == 0)
  249.           colon = index (beg, ':');
  250.         if (colon != 0 && lindex (beg, colon, closeparen) == 0)
  251.           {
  252.         /* This is a substitution reference: $(FOO:A=B).  */
  253.         int count;
  254.         char *subst_beg, *subst_end, *replace_beg, *replace_end;
  255.  
  256.         v = lookup_variable (beg, colon - beg);
  257.  
  258.         subst_beg = colon + 1;
  259.         count = 0;
  260.         for (p = subst_beg; *p != '\0'; ++p)
  261.           {
  262.             if (*p == openparen)
  263.               ++count;
  264.             else if (*p == closeparen)
  265.               --count;
  266.             else if (*p == '=' && count <= 0)
  267.               break;
  268.           }
  269.         if (count > 0)
  270.           /* There were unmatched opening parens.  */
  271.           return initialize_variable_output ();
  272.         subst_end = p;
  273.  
  274.         replace_beg = p + 1;
  275.         count = 0;
  276.         for (p = replace_beg; *p != '\0'; ++p)
  277.           {
  278.             if (*p == openparen)
  279.               ++count;
  280.             else if (*p == closeparen && --count < 0)
  281.               break;
  282.           }
  283.         if (count > 0)
  284.           /* There were unmatched opening parens.  */
  285.           return initialize_variable_output ();
  286.         end = p;
  287.         replace_end = p;
  288.  
  289.         p = expand_argument (subst_beg, subst_end);
  290.         p1 = expand_argument (replace_beg, replace_end);
  291.  
  292.         if (v != 0 && *v->value != '\0')
  293.           {
  294.             char *value = (v->recursive ? recursively_expand (v)
  295.                    : v->value);
  296.             char *percent = find_percent (p);
  297.             if (percent != 0)
  298.               o = patsubst_expand (o, value, p, p1,
  299.                        percent, (char *) 0);
  300.             else
  301.               o = subst_expand (o, value,
  302.                     p, p1, strlen (p), strlen (p1),
  303.                     0, 1);
  304.             if (v->recursive)
  305.               free (value);
  306.           }
  307.  
  308.         free (p);
  309.         free (p1);
  310.           }
  311.  
  312.         /* No, this must be an ordinary variable reference.  */
  313.         else
  314.           {
  315.         /* Look up the value of the variable.  */
  316.         end = index (beg, closeparen);
  317.         if (end == 0)
  318.           return initialize_variable_output ();
  319.         o = reference_variable (o, beg, end - beg);
  320.           }
  321.  
  322.         /* Advance p past the variable reference to resume scan.  */
  323.         p = end;
  324.       }
  325.       break;
  326.  
  327.     case '\0':
  328.       break;
  329.  
  330.     default:
  331.       if (isblank (p[-1]))
  332.         break;
  333.  
  334.       /* A $ followed by a random char is a variable reference:
  335.          $a is equivalent to $(a).  */
  336.       {
  337.         /* We could do the expanding here, but this way
  338.            avoids code repetition at a small performance cost.  */
  339.         char name[5];
  340.         name[0] = '$';
  341.         name[1] = '(';
  342.         name[2] = *p;
  343.         name[3] = ')';
  344.         name[4] = '\0';
  345.         p1 = allocated_variable_expand (name);
  346.         o = variable_buffer_output (o, p1, strlen (p1));
  347.         free (p1);
  348.       }
  349.  
  350.       break;
  351.     }      
  352.  
  353.       if (*p == '\0')
  354.     break;
  355.       else
  356.     ++p;
  357.     }
  358.  
  359.   (void) variable_buffer_output (o, "", 1);
  360.   return initialize_variable_output ();
  361. }
  362.  
  363. /* Expand an argument for an expansion function.
  364.    The text starting at STR and ending at END is variable-expanded
  365.    into a null-terminated string that is returned as the value.
  366.    This is done without clobbering `variable_buffer' or the current
  367.    variable-expansion that is in progress.  */
  368.  
  369. char *
  370. expand_argument (str, end)
  371.      char *str, *end;
  372. {
  373.   char *tmp;
  374.  
  375.   if (*end == '\0')
  376.     tmp = str;
  377.   else
  378.     {
  379.       tmp = (char *) alloca (end - str + 1);
  380.       bcopy (str, tmp, end - str);
  381.       tmp[end - str] = '\0';
  382.     }
  383.  
  384.   return allocated_variable_expand (tmp);
  385. }
  386.  
  387. /* Expand LINE for FILE.  Error messages refer to the file and line where
  388.    FILE's commands were found.  Expansion uses FILE's variable set list.  */
  389.  
  390. char *
  391. variable_expand_for_file (line, file)
  392.      char *line;
  393.      register struct file *file;
  394. {
  395.   char *result;
  396.   struct variable_set_list *save;
  397.  
  398.   if (file == 0)
  399.     return variable_expand (line);
  400.  
  401.   save = current_variable_set_list;
  402.   current_variable_set_list = file->variables;
  403.   reading_filename = file->cmds->filename;
  404.   reading_lineno_ptr = &file->cmds->lineno;
  405.   result = variable_expand (line);
  406.   current_variable_set_list = save;
  407.   reading_filename = 0;
  408.   reading_lineno_ptr = 0;
  409.  
  410.   return result;
  411. }
  412.  
  413. /* Like variable_expand_for_file, but the returned string is malloc'd.
  414.    This function is called a lot.  It wants to be efficient.  */
  415.  
  416. char *
  417. allocated_variable_expand_for_file (line, file)
  418.      char *line;
  419.      struct file *file;
  420. {
  421.   char *value;
  422.  
  423.   char *obuf = variable_buffer;
  424.   unsigned int olen = variable_buffer_length;
  425.  
  426.   variable_buffer = 0;
  427.  
  428.   value = variable_expand_for_file (line, file);
  429.  
  430. #if 0
  431.   /* Waste a little memory and save time.  */
  432.   value = xrealloc (value, strlen (value))
  433. #endif
  434.  
  435.   variable_buffer = obuf;
  436.   variable_buffer_length = olen;
  437.  
  438.   return value;
  439. }
  440.